﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class CMap
    {

        public class CMapData
        {
            public CMapData()
            {

            }
            public CMapData(Point p, CTerrainElement m)
            {
                Position = p;
                data = m;
            }
            public Point Position;
            public CTerrainElement data;
        };

        public List<CMapData> mData = new List<CMapData>();

        public String FileName
        {
            get;
            set;
        }

        public String MainPicName
        {
            get;
            set;
        }

        public void AddData(Point p,CTerrainElement m)
        {
            mData.Add(new CMapData(p, m));
        }


        public String SecondPicName
        {
            get;
            set;
        }

        public bool Read(String fileName)
        {
            FileName = fileName;
            return Read();
        }


        public void Write(String fileName)
        {
            FileName = fileName;
            Write();
        }

        public bool Read()
        {
            using (XmlTextReader xmlread = new XmlTextReader(FileName))
            {
                xmlread.ReadStartElement("Map");
                if (xmlread.IsStartElement("MainPic"))
                {
                    MainPicName = xmlread.ReadString();
                    xmlread.ReadEndElement();
                }
                if (xmlread.IsStartElement("Elements"))
                {
                    int Count = Convert.ToInt32(xmlread.GetAttribute(0));
                    xmlread.Read();
                    for (int i = 0; i < Count; i++)
                    {
                        if (xmlread.IsStartElement("Element"))
                        {
                            String x = xmlread.GetAttribute(0);
                            String y = xmlread.GetAttribute(1);
                            String f = xmlread.GetAttribute(2);

                            CTerrainElement m = new CTerrainElement();
                            if (!m.Read(f))
                            {
                                if (MessageBox.Show("地图元素：" + f + "不能读取，请检查此文件,是否忽略？") == DialogResult.OK)
                                {
                                    continue;
                                }
                                else
                                    return false;
                            }
                            mData.Add(new CMapData(new Point(Convert.ToInt32(x), Convert.ToInt32(y)), m));
                        }
                    }
                }
                return true;
            }
        }

        public void Write()
        {
            XmlTextWriter xmlwrite = new XmlTextWriter(FileName, Encoding.Default);
            xmlwrite.Formatting = Formatting.Indented;
            xmlwrite.WriteStartDocument();
            xmlwrite.WriteStartElement("Map");
            xmlwrite.WriteStartElement("MainPic");
            xmlwrite.WriteString(MainPicName);
            xmlwrite.WriteEndElement();

            xmlwrite.WriteStartElement("Elements");
            xmlwrite.WriteAttributeString("Count",mData.Count.ToString());

            foreach( CMapData i in mData )
            {
                xmlwrite.WriteStartElement("Element");
                xmlwrite.WriteAttributeString("PosX", i.Position.X.ToString());
                xmlwrite.WriteAttributeString("PosY", i.Position.Y.ToString());
                xmlwrite.WriteAttributeString("TerrainElement", i.data.mFileName);
                xmlwrite.WriteEndElement();            
            }

            xmlwrite.WriteEndElement();
            xmlwrite.WriteEndElement();
            xmlwrite.Close();
        }
    }
}
